package model.util;

import jdbc.Function;

import java.util.Random;

/**
 * @author zjw
 * @date 2020/1/4 9:46
 */
public class AuxiliaryCalculationTools {

    public static double getCalculatedWidth(String str) {
        if(str.length() > 40) {
            return 300;
        } else {
            return str.length() * 5;
        }
    }

    public static double getCalculatedHeight(String str) {
        int lineCnt = str.length() % 40;
        int i = 0;
        if(lineCnt != 0){
            i = 1;
        }
        return (str.length() / 40 + i) * 20;
    }

    /**
     *
     * @return 返回一个自动生成的id号
     */
    public static String automaticallyGenerateIdNumber() {
        Random ra = new Random();
        String id = "";
        Function function = new Function();
        while(true) {
            for (int i = 0; i < 5; i++) {
                int num = ra.nextInt(10);
                if(i == 0 && num == 0) {
                    i--;
                    continue;
                }
                id += String.valueOf(num);
            }
            // 检查数据库中是否已经存在该id
            if (function.isIdExist(id) == true) {
                id = "";
                continue;
            }
            break;
        }
        return id;
    }
}
